import {
  Controller,
  Post,
  Body,
  HttpException, HttpStatus, UseInterceptors
} from "@nestjs/common";
import {ApiBody, ApiOperation, ApiTags} from '@nestjs/swagger';
import { UserService } from './user.service';
import {LoginDto, UserListDto, UserUpdateDto, UserAddDto, UserDelDto} from './dto/index';
import { RedisInstance } from '@/common/redis';
import {LoggingInterceptor} from "@/common/interceptor/logging.interceptor";
import { operationTypeEnum } from '@/common/enum';

@ApiTags("用户列表")
@Controller("user")
export class UserController {

  private redis:RedisInstance
  constructor(private readonly userService:UserService) {
    this.redis = new RedisInstance("user")
  }

  //apiBody怎么用？
  @ApiBody({type:UserListDto})
  @Post("/userList")
  @ApiOperation({description:"用户列表"})
  @UseInterceptors(new LoggingInterceptor({data:"获取用户列表",logType:"operationLog",operationType:operationTypeEnum.search}))
  userList(@Body() userListDto:UserListDto){
    return this.userService.userList(userListDto);
  }

  @Post("/userUpdate")
  @ApiOperation({description:"用户更新"})
  @UseInterceptors(new LoggingInterceptor({data:"用户更新",logType:"operationLog",operationType:operationTypeEnum.edit}))
  userUpdate(@Body() userUpdateDto: UserUpdateDto) {
    return this.userService.userUpdate(userUpdateDto);
  }

  @Post("/userDel")
  @ApiOperation({description:"用户删除"})
  @UseInterceptors(new LoggingInterceptor({data:"用户删除",logType:"operationLog",operationType:operationTypeEnum.delete}))
  userDel(@Body() userDelDto: UserDelDto) {
    return this.userService.delete(userDelDto.id);
  }

  @Post("/userAdd")
  @ApiOperation({description:"用户添加"})
  @UseInterceptors(new LoggingInterceptor({data:"用户添加",logType:"operationLog",operationType:operationTypeEnum.add}))
  userAdd(@Body() userAddDto: UserAddDto) {
    return this.userService.userAdd(userAddDto);
  }

  @Post("/userInfoDetail")
  @ApiOperation({description:"用户详情"})
  @UseInterceptors(new LoggingInterceptor({data:"用户详情",logType:"operationLog",operationType:operationTypeEnum.search}))
  userInfoDetail(@Body("uid") uid) {
    return this.userService.userDetailByUserId(uid);
  }

  @Post("/login")
  @ApiOperation({description:"登录"})
  @UseInterceptors(new LoggingInterceptor({data:"登录",logType:"loginLog",operationType:operationTypeEnum.other}))
  async login(@Body() LoginDto:LoginDto){
    let {username,password} = LoginDto
    return await this.userService.login({ username, password });
  }
  @Post("/userInfo")
  @ApiOperation({description:"获取用户信息"})
  @UseInterceptors(new LoggingInterceptor({data:"获取用户信息",logType:"operationLog",operationType:operationTypeEnum.search}))
  async userInfo(@Body() body){
    let token = body.token??""
    if(!token){
      throw new HttpException('token不能为空', HttpStatus.BAD_GATEWAY);
    }
    return await this.userService.userInfo(token);
  }


  @Post('/userChangeStatus')
  @ApiOperation({description:"修改用户状态"})
  @UseInterceptors(new LoggingInterceptor({data:"修改用户状态",logType:"operationLog",operationType:operationTypeEnum.edit}))
  userChangeStatus(@Body("id") id: number,@Body("status") status:string|number) {
    return this.userService.userChangeStatus(id,status);
  }

}
